package com.photoeditor.demo.ui.widget.collage;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import com.photoeditor.R;
import com.photoeditor.demo.model.image.collage.templet.DragPoint;
import com.photoeditor.demo.model.image.collage.templet.Point;
import com.photoeditor.demo.util.DisplayUtils;

import java.util.List;

/**
 * 拼图界面绘制用于拖拽的点
 * Created by edward on 17-9-21.
 */

public class DragPointView extends View {
    private static final int RADIUS = DisplayUtils.dp2px(10);
    private static final int MAX_TOUCH_OFFSET = DisplayUtils.dp2px(20);
    private List<DragPoint> mDragPointList;
    private Bitmap mBitmap;

    private Paint mDragPointPaint;

    public DragPointView(Context context) {
        super(context);
        mDragPointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mDragPointPaint.setColor(Color.YELLOW);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_layout_resize);
        mBitmap = Bitmap.createScaledBitmap(mBitmap, RADIUS * 2, RADIUS * 2, false);
    }

    public void setDragPointList(List<DragPoint> dragPointList) {
        mDragPointList = dragPointList;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mDragPointList != null && mDragPointList.size() > 0) {
            for (DragPoint point : mDragPointList) {
                canvas.save();
                canvas.translate(point.x - RADIUS, point.y - RADIUS);
                if (point.orientation == DragPoint.Orientation.horizontal) {
                    canvas.rotate(90);
                    canvas.translate(0, -2 * RADIUS);
                }
                canvas.drawBitmap(mBitmap, 0, 0, mDragPointPaint);
                canvas.restore();
            }
        }
    }

    /**
     * 判断按下点距离最近的点的偏移，距离的最大值为{@link #MAX_TOUCH_OFFSET},超过这个值视为没有点中，返回-1
     *
     * @param touchPoint
     * @return 距离最近的点的下标
     */
    public int getTouchOffset(android.graphics.Point touchPoint, Point outOffset) {
        if (touchPoint == null || outOffset == null) {
            return -1;
        }
        int minOffsetIndex = -1;
        float minDistance = -1;
        if (mDragPointList != null && mDragPointList.size() > 0) {
            for (int i = 0; i < mDragPointList.size(); i++) {
                float distance = (float) Math.sqrt(Math.pow(Math.abs(touchPoint.x - mDragPointList.get(i).x), 2) +
                        Math.pow(Math.abs(touchPoint.y - mDragPointList.get(i).y), 2));
                if (minDistance == -1 || distance < minDistance) {
                    minDistance = distance;
                    minOffsetIndex = i;
                }
            }
        }
        if (minDistance <= MAX_TOUCH_OFFSET && minOffsetIndex != -1) {
            outOffset.set(mDragPointList.get(minOffsetIndex).x - touchPoint.x,
                    mDragPointList.get(minOffsetIndex).y - touchPoint.y);
            return minOffsetIndex;
        }
        return -1;
    }
}
